package top.wshape1.shiziapi.project.controller;

import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.wshape1.shiziapi.common.constant.AuthConstant;
import top.wshape1.shiziapi.common.domain.CommonUser;
import top.wshape1.shiziapi.common.utils.Base64Util;
import top.wshape1.shiziapi.common.utils.R;
import top.wshape1.shiziapi.project.domain.vo.TwoIdVo;
import top.wshape1.shiziapi.project.domain.vo.SaveContentVo;
import top.wshape1.shiziapi.project.service.RtfService;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-11-04
 */

@RestController
@RequestMapping("/rtf")
public class RtfController {

    @Resource
    private RtfService rtfService;

    /**
     * 获取内容
     *
     * @param contentVo
     * @param userBase64
     * @return
     */
    @PostMapping("/content")
    public R<String> getContent(@RequestBody @Validated TwoIdVo contentVo,
                                @RequestHeader(value = AuthConstant.HEADER_PAYLOAD_NAME, required = false) String userBase64) {

        Long userId = null;
        if (userBase64 != null)
            userId = Base64Util.decodeAndParse(userBase64, CommonUser.class).getId();

        String content = rtfService.getContent(contentVo, userId);

        return R.ok().putData(content);
    }

    /**
     * 保存内容
     *
     * @param contentVo
     * @param userBase64
     * @return
     */
    @PostMapping("/content/save")
    public R<Void> saveContent(@RequestBody @Validated SaveContentVo contentVo,
                         @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        rtfService.SaveContent(contentVo, commonUser.getId());

        return R.ok();
    }

}
